﻿using AiMoCore;
using System;
using System.Reflection;

namespace AiMoRhino.Main
{
    class CompiledAssemblyInfo : IDisposable
    {
        public string Path { get; }
        public CSharpScriptInstance ScriptInstance { get; }
        CompiledAssemblyInfo(string path, CSharpScriptInstance instance)
        {
            Path = path;
            ScriptInstance = instance;
        }
        public void Dispose()
        {
            ScriptInstance.Dispose();
        }
        public static bool TryGetCompiledAssemblyInfo(string assemblyPath, string[] externalAssemblies, out CompiledAssemblyInfo result, out string error)
        {
            error = "";
            result = null;
            // 加载主dll之前，先加载附属的dll
            //bool loadExternalSuccess = true;
            foreach (string externalAssembly in externalAssemblies)
            {
                try
                {
                    Assembly.LoadFrom(externalAssembly);
                }
                catch (Exception ex)
                {
                    error += $"Cannot load external assemly\r\n   {externalAssembly}\r\n   {ex.Message}\r\n   {ex.StackTrace}\r\n"; ;
                    //loadExternalSuccess = false;
                }
            }

            Assembly assembly;
            try
            {
                assembly = Assembly.LoadFrom(assemblyPath);
            }
            catch (Exception ex)
            {
                error += $"Exception: \r\n   {ex.Message}\r\n   {ex.StackTrace}"; ;
                return false;
            }
            try
            {
                object objectValue = assembly.CreateInstance("Script_Instance");

                if (objectValue is CSharpScriptInstance instance)
                {
                    result = new CompiledAssemblyInfo(assemblyPath, instance);
                    return true;
                }
                else
                {
                    error += "Type Script_Instance could not be found in assembly";
                    return false;
                }

                //Type type = assembly.GetType("Script_Instance");
                //if (type == null)
                //{
                //    error += "Type Script_Instance could not be found in assembly";
                //    return false;
                //}
                //object objectValue = RuntimeHelpers.GetObjectValue(Activator.CreateInstance(type));
                //if (objectValue == null)
                //{
                //    error += "Cannot convert Instance type to CSharpScriptInstance";
                //    return false;
                //}
                //result = new CompiledAssemblyInfo(assemblyPath, (CSharpScriptInstance)objectValue);
                //return true;
            }
            catch (Exception ex)
            {
                error += $"Exception: Cannot get type CSharpScriptInstance\r\n   {ex.Message}\r\n   {ex.StackTrace}";
                return false;
            }
        }
    }
}
